package tq.com.tqteacher.view;

import android.app.AlertDialog;
import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.CustomListener;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.contrarywind.interfaces.IPickerViewData;

import java.util.ArrayList;

import tq.com.systools.system.AlertDialogHelper;
import tq.com.systools.system.StringHelper;
import tq.com.tqcom.util.TQSystemDefine;
import tq.com.tqteacher.R;
import tq.com.tqteacher.model.TQNewPublish;

public class TQBasePublishInfoView extends FrameLayout {

    private EditText titleView;
    private EditText descView;
    private EditText gradView;
    private OptionsPickerView mOptionsPickerView;
    private ArrayList<GradData> info = new ArrayList<>();
    private GradData currentGrade;

    static private String TAG = "TQBasePublishInfoView";

    public TQBasePublishInfoView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(attrs);
    }

    protected void initView(AttributeSet pAttributeSet){
        LayoutInflater.from(getContext()).inflate(R.layout.view_basepublishinfo_layout, this, true);

        TypedArray typedArray = getContext().obtainStyledAttributes(pAttributeSet, R.styleable.TQBasePublishInfoView);
        boolean showDesc = typedArray.getBoolean(R.styleable.TQBasePublishInfoView_TQBasePublishInfoView_showDesc, true);
        findViewById(R.id.desc_panel).setVisibility(showDesc ? VISIBLE : INVISIBLE);
        typedArray.recycle();

        titleView = findViewById(R.id.title_input);
        descView = findViewById(R.id.desc_input);
        gradView = findViewById(R.id.grad_input);

        gradView.setFocusable(false);
        gradView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "onClick: ");
                if(info.size() > 1){
                    mOptionsPickerView.show();
                }

            }
        });


        mOptionsPickerView = new OptionsPickerBuilder(getContext(), new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                Log.d(TAG, "onOptionsSelect: ");

                GradData lGradData = info.get(options1);
                gradView.setText(lGradData.text);

                currentGrade = lGradData;
            }
        }).isDialog(true).setLayoutRes(R.layout.view_choice_layout, new CustomListener() {
            @Override
            public void customLayout(View v) {
                v.findViewById(R.id.btnSubmit).setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mOptionsPickerView.returnData();
                        mOptionsPickerView.dismiss();
                    }
                });

                v.findViewById(R.id.btnCancel).setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mOptionsPickerView.dismiss();
                    }
                });
            }
        }).build();

        initDefaultGrad();
    }

    public String title(){
        return StringHelper.editText(titleView);
    }

    public String desc(){
        return StringHelper.editText(descView);
    }
    public String grad(){
        return StringHelper.editText(gradView);
    }

    public boolean checkValid(){
        if(!StringHelper.hasText(titleView)){
            AlertDialogHelper.show(this.getContext(), "标题不能为空");
            return false;
        }

        if(!StringHelper.hasText(descView)){
            AlertDialogHelper.show(this.getContext(), "描述不能为空");
            return false;
        }

        if(!StringHelper.hasText(gradView)){
            AlertDialogHelper.show(this.getContext(), "年级不能为空");
            return false;
        }

        return true;
    }

    public void setGradData(ArrayList<GradData> pData){
        info.clear();
        info.addAll(pData);

        if(info.size() > 0){
            GradData lData = info.get(0);
            gradView.setText(lData.text);
            currentGrade = lData;
        }
    }
    public void setPublishContent(TQNewPublish pPublish){
        if(pPublish == null) return;

        titleView.setText(pPublish.getName());
        descView.setText(pPublish.getDesc());

        gradView.setText(TQSystemDefine.gradeName(pPublish.getGrade()));
    }

    public GradData getCurrentGrade(){
        return currentGrade;
    }

    private void initDefaultGrad(){
        ArrayList<String> all = TQSystemDefine.allGradeName(0);


        int i = 1;
        for (String item:
             all) {

            GradData g = new GradData(i, item);
            info.add(g);
            i++;
        }

        mOptionsPickerView.setPicker(all);

        GradData lData = info.get(0);
        gradView.setText(lData.text);
        currentGrade = lData;
    }

    static public class GradData implements IPickerViewData {
        private String text;
        private int Id;

        public GradData(int id, String txt){
            text = txt;
            this.Id = id;
        }

        public String getPickerViewText(){
            return text;
        }

        public int getId(){
            return Id;
        }
    }


    private TextWatcher mTextWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    };
}
